package ru.kest.babymonitor;


import android.util.Log;

import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

public class CamMovement {

    private static String TAG = "IPCamBabyMonitor";

    private final static int CONNECTION_TIMEOUT = 4000;

    private static void move(final int command) {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URLConnection conn;
                    InputStream is = null;

                    try {
                        conn = new URL("http://192.168.88.111/decoder_control.cgi?loginuse=admin&loginpas=&onestep=2&command="+command).openConnection();
                        conn.setConnectTimeout(CONNECTION_TIMEOUT);
                        conn.setReadTimeout(CONNECTION_TIMEOUT);
                        is = conn.getInputStream();

                        Log.i(TAG, "Move cam. Command = " + command);
                    } finally {
                        if (is != null) is.close();
                    }
                } catch (Exception e) {
                    Log.e(TAG, "Movement error: " + e.getMessage());
                }
            }
        });
        thread.start();
    }

    public static void moveUp() {
        move(2);
    }

    public static void moveDown() {
        move(0);
    }

    public static void moveLeft() {
        move(6);
    }

    public static void moveRight() {
        move(4);
    }
}
